package com.zwface.demo.base;

import android.app.ProgressDialog;
import android.os.Handler;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.zwface.demo.App;
import com.zwface.demo.comm.comm;
import com.zwface.demo.utils.SharedPreferenceUtils;

import java.util.HashMap;
import java.util.Map;

import androidx.databinding.ObservableBoolean;
import androidx.lifecycle.ViewModel;

/**
 * created by 张中文
 * on 2020/2/18
 * 描述：
 */
public class BaseViewModel extends ViewModel
{
    private String userType =  SharedPreferenceUtils.getParam(App.getInstance().getApplicationContext(),SharedPreferenceUtils.USER_TYPE);
    public ObservableBoolean isTeacher = new ObservableBoolean(userType.equals(comm.USER_TYPE_TEA+""));

    /**
     * 防止快速请求两次
     */
    Map<String,Long> requestTimeStampHashMap = new HashMap<String, Long>(); //上一次请求时间
    int limitRequestTime = 600;   //400毫秒
    public Boolean isDoubleClick(String requestMethodName)
    {      //防止用户连续点击按钮调用
        if (!requestTimeStampHashMap.containsKey(requestMethodName)) {
            requestTimeStampHashMap.put(requestMethodName,0L);
        }

        Boolean res =false;
        if ((System.currentTimeMillis() - requestTimeStampHashMap.get(requestMethodName)) > limitRequestTime)
        {
            requestTimeStampHashMap.put(requestMethodName,System.currentTimeMillis());
            res = false;
        } else {
            res = true;
        }
        return res;
    }

    public void showToast(String msg)
    {
        ToastUtils.showShort(msg);
    }

    public void showLongToast(String msg)
    {
        ToastUtils.showLong(msg);
    }

    public void cancelToast()
    {
        ToastUtils.cancel();
    }

    public void showLoading(String text) {
        showLoading(text,false);
    }

    public void showLoading(String text, Boolean cancleable) {
        App.getInstance().progressDialog = new ProgressDialog(ActivityUtils.getTopActivity());
        App.getInstance().progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        App.getInstance().progressDialog.setMessage(StringUtils.isEmpty(text)?"":text);    //设置内容
        App.getInstance().progressDialog.setCancelable(cancleable);//点击屏幕和按返回键都不能取消加载框
        App.getInstance().progressDialog.show();

        //设置超时自动消失
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //取消加载框
                if(dismissLoading()){
                    //超时处理
                    ToastUtils.showShort("请求超时");
                }
            }
        }, 30000);//超时时间30秒
    }

    public Boolean dismissLoading() {
        if (App.getInstance().progressDialog != null){
            if (App.getInstance().progressDialog.isShowing()) {
                App.getInstance().progressDialog.dismiss();
                App.getInstance().progressDialog = null;
                return true;//取消成功
            }
        }
        return false;//已经取消过了，不需要取消
    }
}
